from dl_music import dl_music
import os
import time
from param import *
from publib import setup_logger, get_logger, csv_read_list, csv_write_list

cur_file = os.path.basename(__file__).split(".")[0]
cur_path = os.path.dirname(os.path.abspath(__file__))
logger = get_logger(__name__)

def dl_task():
    path = song_req_cache_file
    list = csv_read_list(path)
    for item in list:
        song = item[0]
        src = item[1]
        id = item[2]
        mid = item[3]
        status = item[4]
        if (status != "error"):
            ret = dl_music(src, song, id, mid)
            if (ret == True):
                print(f"remove {item}")
                list.remove(item)
                csv_write_list(path, list, "w")
                break
            else:
                print(f"{item} error")
                item[4] = "error"
                csv_write_list(path, list, "w")

if __name__ == "__main__":
    setup_logger(f"{cur_path}/logs/{cur_file}.log")
    logger.info("dl task start")
    while (1):
        try:
            dl_task()
            time.sleep(30)
        except Exception as e:
            logger.error(f"run dl task error: {e}")
            time.sleep(3600)
